Training Object Detection Model Using YoloV8¶

The model is trained on a venv setup in pycharm

In [ ]:
# Authors : Anish Krishnan - 8875914, Samarth S Deshpande - 8894058

# Importing Required Libraries
from ultralytics import YOLO
import torch


# MOdel Training Code (Do not Run without installing dependencies)
def run():
    torch.multiprocessing.freeze_support()
    print('loop')

if __name__ == '__main__':
    run()
    # Loading Pre Trained Model and Weight Trained on Coco dataset
    model = YOLO('yolov8n.yaml')
    model = YOLO('yolov8n.pt')  # load a pretrained model (recommended for training)
    model = YOLO('yolov8n.yaml').load('yolov8n.pt')  # build from YAML and transfer wei

    results = model.train(data=r"C:\Users\krish\OneDrive\Desktop\project\vehicle.yaml", epochs=100, optimizer='AdamW',
                          batch=2, resume = True)
In [29]:
# Model Input Configuration
import yaml

with open(r"C:\Users\krish\OneDrive\Desktop\project\vehicle.yaml", "r") as stream:
    print(yaml.safe_load(stream))
{'path': 'C:/Users/krish/OneDrive/Desktop/project/data/dataset', 'train': 'train/images', 'val': 'val/images', 'test': 'test/images', 'nc': 5, 'names': ['Car', 'Motorcycle', 'Truck', 'Bus', 'Bicycle']}

Distribution of the Target Classes¶

image.png

Bar Chart: The top left graph is a bar chart showing the count (instances) of different types of vehicles: buses, cars, motorcycles, and trucks. The y-axis represents the number of instances, and the x-axis represents the vehicle type. The height of each bar reflects the number of instances of each vehicle type. In this case, cars have the highest count by a significant margin, followed by trucks, motorcycles, and buses.

Box Plot: The top right graph appears to be a series of box plots overlaid on each other, possibly representing the distribution of a particular variable across different categories. The box plot typically shows the median, quartiles, and potential outliers for a set of data. However, the details are not clear from this image, as it seems to show an unusual amount of overlap or a high density of box plots.

Heatmap: The bottom left graph is a heatmap, which uses color coding to represent the magnitude of a phenomenon as a matrix. In this graph, the x and y-axes represent some sort of numerical scale, and the intensity of the color indicates the concentration or amount of a certain variable at each point in the matrix. Darker colors usually represent higher values. This particular heatmap shows a concentration of values in the center.

Scatter Plot Histogram: The bottom right graph is a scatter plot histogram, also known as a 2D histogram or hexbin plot, which displays the frequency of occurrences within bins in two dimensions. The x-axis represents 'width' and the y-axis represents 'height'. The plot uses shading to represent the density of points in different regions, with darker colors typically indicating a higher concentration of points. This plot shows a high concentration of points in the lower-left corner.

We can clearly observe, the class towards car is biased. It has significantly more data than the other classes.

The other plots are density plot of how the bounding boxes are distributed .

image.png

image.png

Confusion Matrix and Precision, Recall and F1¶

It is based on the validation testing done during the training process¶

image.png

Diagonal Cells: The numbers on the diagonal from the top left to the bottom right represent the number of correct predictions made by the model for each class. For instance, the model correctly predicted 'car' 2832 times.

Off-Diagonal Cells: The off-diagonal numbers show the instances that were misclassified by the model. For example, the model predicted 'car' when the actual class was 'bus' 94 times.

Classes: This particular confusion matrix has five classes: bus, car, motorcycle, truck, and background. These could represent different categories that an image classification model is trying to predict.

Top Row: The top row indicates the true classes of the samples.

First Column: The first column on the left indicates the predicted classes by the model.

Color Coding: The intensity of the color corresponds to the number of occurrences, with darker shades typically indicating higher numbers. This visual aid helps to quickly identify which classes are most often confused.

Interpretation: For example, looking at the 'motorcycle' column and row, we can see that motorcycles were correctly predicted 448 times, but 13 motorcycles were misclassified as cars, 4 as trucks, and 457 were misclassified as background. Similarly, 1,319 cars were misclassified as motorcycles, and 1388 trucks were misclassified as background.

image.png image-2.png image-3.png

Running Inference on an Image¶

In [1]:
# https://docs.ultralytics.com/usage/cfg/


from ultralytics import YOLO
from glob import glob
#car = YOLO('yolov8m')
car = YOLO(r"C:\Users\krish\PycharmProjects\AIFundamentals\runs\detect\train14\weights\last.pt")
In [2]:
files = glob(r'C:\Users\krish\OneDrive\Desktop\project\data\new_car\data\test\*jpg')
In [3]:
# Getting the predicted bounding boxes from the test images
predicted_boxes = []
for i in files:
    results = car.predict(i, save = True)
    for r in results: 
        predicted_boxes.append(r.boxes.xyxy)
        
image 1/1 C:\Users\krish\OneDrive\Desktop\project\data\new_car\data\test\vid_4_1000.jpg: 384x640 1 car, 193.1ms
Speed: 7.2ms preprocess, 193.1ms inference, 100.6ms postprocess per image at shape (1, 3, 384, 640)
Results saved to runs\detect\predict10

image 1/1 C:\Users\krish\OneDrive\Desktop\project\data\new_car\data\test\vid_4_1020.jpg: 384x640 1 car, 19.0ms
Speed: 2.0ms preprocess, 19.0ms inference, 2.0ms postprocess per image at shape (1, 3, 384, 640)
Results saved to runs\detect\predict10

image 1/1 C:\Users\krish\OneDrive\Desktop\project\data\new_car\data\test\vid_4_1040.jpg: 384x640 1 car, 18.0ms
Speed: 2.0ms preprocess, 18.0ms inference, 3.0ms postprocess per image at shape (1, 3, 384, 640)
Results saved to runs\detect\predict10

image 1/1 C:\Users\krish\OneDrive\Desktop\project\data\new_car\data\test\vid_4_1060.jpg: 384x640 2 cars, 17.5ms
Speed: 2.0ms preprocess, 17.5ms inference, 2.0ms postprocess per image at shape (1, 3, 384, 640)
Results saved to runs\detect\predict10

image 1/1 C:\Users\krish\OneDrive\Desktop\project\data\new_car\data\test\vid_4_1080.jpg: 384x640 1 car, 22.0ms
Speed: 2.0ms preprocess, 22.0ms inference, 3.0ms postprocess per image at shape (1, 3, 384, 640)
Results saved to runs\detect\predict10

image 1/1 C:\Users\krish\OneDrive\Desktop\project\data\new_car\data\test\vid_4_1520.jpg: 384x640 (no detections), 18.0ms
Speed: 2.0ms preprocess, 18.0ms inference, 1.0ms postprocess per image at shape (1, 3, 384, 640)
Results saved to runs\detect\predict10

image 1/1 C:\Users\krish\OneDrive\Desktop\project\data\new_car\data\test\vid_4_1540.jpg: 384x640 (no detections), 13.4ms
Speed: 2.0ms preprocess, 13.4ms inference, 2.0ms postprocess per image at shape (1, 3, 384, 640)
Results saved to runs\detect\predict10

image 1/1 C:\Users\krish\OneDrive\Desktop\project\data\new_car\data\test\vid_4_1560.jpg: 384x640 (no detections), 13.8ms
Speed: 2.0ms preprocess, 13.8ms inference, 1.0ms postprocess per image at shape (1, 3, 384, 640)
Results saved to runs\detect\predict10

image 1/1 C:\Users\krish\OneDrive\Desktop\project\data\new_car\data\test\vid_4_1580.jpg: 384x640 (no detections), 15.5ms
Speed: 2.0ms preprocess, 15.5ms inference, 2.0ms postprocess per image at shape (1, 3, 384, 640)
Results saved to runs\detect\predict10

image 1/1 C:\Users\krish\OneDrive\Desktop\project\data\new_car\data\test\vid_4_1600.jpg: 384x640 (no detections), 13.0ms
Speed: 2.0ms preprocess, 13.0ms inference, 1.0ms postprocess per image at shape (1, 3, 384, 640)
Results saved to runs\detect\predict10

image 1/1 C:\Users\krish\OneDrive\Desktop\project\data\new_car\data\test\vid_4_1620.jpg: 384x640 (no detections), 15.0ms
Speed: 2.0ms preprocess, 15.0ms inference, 2.0ms postprocess per image at shape (1, 3, 384, 640)
Results saved to runs\detect\predict10

image 1/1 C:\Users\krish\OneDrive\Desktop\project\data\new_car\data\test\vid_4_1640.jpg: 384x640 (no detections), 13.5ms
Speed: 2.0ms preprocess, 13.5ms inference, 1.0ms postprocess per image at shape (1, 3, 384, 640)
Results saved to runs\detect\predict10

image 1/1 C:\Users\krish\OneDrive\Desktop\project\data\new_car\data\test\vid_4_1660.jpg: 384x640 (no detections), 15.0ms
Speed: 1.0ms preprocess, 15.0ms inference, 1.0ms postprocess per image at shape (1, 3, 384, 640)
Results saved to runs\detect\predict10

image 1/1 C:\Users\krish\OneDrive\Desktop\project\data\new_car\data\test\vid_4_1700.jpg: 384x640 (no detections), 15.0ms
Speed: 1.0ms preprocess, 15.0ms inference, 1.0ms postprocess per image at shape (1, 3, 384, 640)
Results saved to runs\detect\predict10

image 1/1 C:\Users\krish\OneDrive\Desktop\project\data\new_car\data\test\vid_4_1740.jpg: 384x640 (no detections), 14.0ms
Speed: 1.0ms preprocess, 14.0ms inference, 1.0ms postprocess per image at shape (1, 3, 384, 640)
Results saved to runs\detect\predict10

image 1/1 C:\Users\krish\OneDrive\Desktop\project\data\new_car\data\test\vid_4_1760.jpg: 384x640 1 car, 14.5ms
Speed: 2.0ms preprocess, 14.5ms inference, 2.0ms postprocess per image at shape (1, 3, 384, 640)
Results saved to runs\detect\predict10

image 1/1 C:\Users\krish\OneDrive\Desktop\project\data\new_car\data\test\vid_4_1780.jpg: 384x640 2 cars, 18.0ms
Speed: 1.1ms preprocess, 18.0ms inference, 2.0ms postprocess per image at shape (1, 3, 384, 640)
Results saved to runs\detect\predict10

image 1/1 C:\Users\krish\OneDrive\Desktop\project\data\new_car\data\test\vid_4_1800.jpg: 384x640 4 cars, 16.0ms
Speed: 1.0ms preprocess, 16.0ms inference, 3.0ms postprocess per image at shape (1, 3, 384, 640)
Results saved to runs\detect\predict10

image 1/1 C:\Users\krish\OneDrive\Desktop\project\data\new_car\data\test\vid_4_1820.jpg: 384x640 2 cars, 15.0ms
Speed: 2.0ms preprocess, 15.0ms inference, 2.0ms postprocess per image at shape (1, 3, 384, 640)
Results saved to runs\detect\predict10

image 1/1 C:\Users\krish\OneDrive\Desktop\project\data\new_car\data\test\vid_4_1840.jpg: 384x640 2 cars, 13.0ms
Speed: 1.0ms preprocess, 13.0ms inference, 2.0ms postprocess per image at shape (1, 3, 384, 640)
Results saved to runs\detect\predict10

image 1/1 C:\Users\krish\OneDrive\Desktop\project\data\new_car\data\test\vid_4_1860.jpg: 384x640 2 cars, 14.0ms
Speed: 2.0ms preprocess, 14.0ms inference, 2.0ms postprocess per image at shape (1, 3, 384, 640)
Results saved to runs\detect\predict10

image 1/1 C:\Users\krish\OneDrive\Desktop\project\data\new_car\data\test\vid_4_1880.jpg: 384x640 6 cars, 16.0ms
Speed: 2.0ms preprocess, 16.0ms inference, 3.0ms postprocess per image at shape (1, 3, 384, 640)
Results saved to runs\detect\predict10

image 1/1 C:\Users\krish\OneDrive\Desktop\project\data\new_car\data\test\vid_4_1900.jpg: 384x640 1 car, 17.0ms
Speed: 2.0ms preprocess, 17.0ms inference, 2.0ms postprocess per image at shape (1, 3, 384, 640)
Results saved to runs\detect\predict10

image 1/1 C:\Users\krish\OneDrive\Desktop\project\data\new_car\data\test\vid_4_1920.jpg: 384x640 2 cars, 18.2ms
Speed: 2.0ms preprocess, 18.2ms inference, 2.0ms postprocess per image at shape (1, 3, 384, 640)
Results saved to runs\detect\predict10

image 1/1 C:\Users\krish\OneDrive\Desktop\project\data\new_car\data\test\vid_4_1940.jpg: 384x640 2 cars, 22.0ms
Speed: 2.4ms preprocess, 22.0ms inference, 2.0ms postprocess per image at shape (1, 3, 384, 640)
Results saved to runs\detect\predict10

image 1/1 C:\Users\krish\OneDrive\Desktop\project\data\new_car\data\test\vid_4_1960.jpg: 384x640 3 cars, 13.0ms
Speed: 2.0ms preprocess, 13.0ms inference, 3.0ms postprocess per image at shape (1, 3, 384, 640)
Results saved to runs\detect\predict10

image 1/1 C:\Users\krish\OneDrive\Desktop\project\data\new_car\data\test\vid_4_1980.jpg: 384x640 5 cars, 14.0ms
Speed: 2.0ms preprocess, 14.0ms inference, 1.5ms postprocess per image at shape (1, 3, 384, 640)
Results saved to runs\detect\predict10

image 1/1 C:\Users\krish\OneDrive\Desktop\project\data\new_car\data\test\vid_4_2000.jpg: 384x640 8 cars, 15.0ms
Speed: 2.0ms preprocess, 15.0ms inference, 3.0ms postprocess per image at shape (1, 3, 384, 640)
Results saved to runs\detect\predict10

image 1/1 C:\Users\krish\OneDrive\Desktop\project\data\new_car\data\test\vid_4_2020.jpg: 384x640 6 cars, 13.0ms
Speed: 2.0ms preprocess, 13.0ms inference, 4.0ms postprocess per image at shape (1, 3, 384, 640)
Results saved to runs\detect\predict10

image 1/1 C:\Users\krish\OneDrive\Desktop\project\data\new_car\data\test\vid_4_2040.jpg: 384x640 5 cars, 14.0ms
Speed: 1.5ms preprocess, 14.0ms inference, 3.0ms postprocess per image at shape (1, 3, 384, 640)
Results saved to runs\detect\predict10

image 1/1 C:\Users\krish\OneDrive\Desktop\project\data\new_car\data\test\vid_4_2060.jpg: 384x640 5 cars, 15.0ms
Speed: 2.0ms preprocess, 15.0ms inference, 2.0ms postprocess per image at shape (1, 3, 384, 640)
Results saved to runs\detect\predict10

image 1/1 C:\Users\krish\OneDrive\Desktop\project\data\new_car\data\test\vid_4_2080.jpg: 384x640 7 cars, 19.0ms
Speed: 1.0ms preprocess, 19.0ms inference, 2.6ms postprocess per image at shape (1, 3, 384, 640)
Results saved to runs\detect\predict10

image 1/1 C:\Users\krish\OneDrive\Desktop\project\data\new_car\data\test\vid_4_2100.jpg: 384x640 4 cars, 15.0ms
Speed: 2.0ms preprocess, 15.0ms inference, 3.0ms postprocess per image at shape (1, 3, 384, 640)
Results saved to runs\detect\predict10

image 1/1 C:\Users\krish\OneDrive\Desktop\project\data\new_car\data\test\vid_4_2120.jpg: 384x640 3 cars, 16.0ms
Speed: 2.0ms preprocess, 16.0ms inference, 3.0ms postprocess per image at shape (1, 3, 384, 640)
Results saved to runs\detect\predict10

image 1/1 C:\Users\krish\OneDrive\Desktop\project\data\new_car\data\test\vid_4_2140.jpg: 384x640 5 cars, 14.0ms
Speed: 2.0ms preprocess, 14.0ms inference, 2.0ms postprocess per image at shape (1, 3, 384, 640)
Results saved to runs\detect\predict10

image 1/1 C:\Users\krish\OneDrive\Desktop\project\data\new_car\data\test\vid_4_2160.jpg: 384x640 3 cars, 13.5ms
Speed: 3.0ms preprocess, 13.5ms inference, 2.0ms postprocess per image at shape (1, 3, 384, 640)
Results saved to runs\detect\predict10

image 1/1 C:\Users\krish\OneDrive\Desktop\project\data\new_car\data\test\vid_4_2180.jpg: 384x640 1 car, 14.5ms
Speed: 1.0ms preprocess, 14.5ms inference, 3.0ms postprocess per image at shape (1, 3, 384, 640)
Results saved to runs\detect\predict10

image 1/1 C:\Users\krish\OneDrive\Desktop\project\data\new_car\data\test\vid_4_2200.jpg: 384x640 1 car, 13.0ms
Speed: 1.0ms preprocess, 13.0ms inference, 2.0ms postprocess per image at shape (1, 3, 384, 640)
Results saved to runs\detect\predict10

image 1/1 C:\Users\krish\OneDrive\Desktop\project\data\new_car\data\test\vid_4_2220.jpg: 384x640 (no detections), 14.0ms
Speed: 2.0ms preprocess, 14.0ms inference, 1.0ms postprocess per image at shape (1, 3, 384, 640)
Results saved to runs\detect\predict10

image 1/1 C:\Users\krish\OneDrive\Desktop\project\data\new_car\data\test\vid_4_2240.jpg: 384x640 (no detections), 15.7ms
Speed: 1.0ms preprocess, 15.7ms inference, 1.0ms postprocess per image at shape (1, 3, 384, 640)
Results saved to runs\detect\predict10

image 1/1 C:\Users\krish\OneDrive\Desktop\project\data\new_car\data\test\vid_4_2260.jpg: 384x640 (no detections), 15.5ms
Speed: 2.0ms preprocess, 15.5ms inference, 1.0ms postprocess per image at shape (1, 3, 384, 640)
Results saved to runs\detect\predict10

image 1/1 C:\Users\krish\OneDrive\Desktop\project\data\new_car\data\test\vid_4_2280.jpg: 384x640 (no detections), 14.5ms
Speed: 2.0ms preprocess, 14.5ms inference, 1.0ms postprocess per image at shape (1, 3, 384, 640)
Results saved to runs\detect\predict10

image 1/1 C:\Users\krish\OneDrive\Desktop\project\data\new_car\data\test\vid_4_2300.jpg: 384x640 (no detections), 14.0ms
Speed: 2.0ms preprocess, 14.0ms inference, 1.0ms postprocess per image at shape (1, 3, 384, 640)
Results saved to runs\detect\predict10

image 1/1 C:\Users\krish\OneDrive\Desktop\project\data\new_car\data\test\vid_4_2320.jpg: 384x640 (no detections), 14.0ms
Speed: 2.0ms preprocess, 14.0ms inference, 1.0ms postprocess per image at shape (1, 3, 384, 640)
Results saved to runs\detect\predict10

image 1/1 C:\Users\krish\OneDrive\Desktop\project\data\new_car\data\test\vid_4_2340.jpg: 384x640 (no detections), 16.0ms
Speed: 2.0ms preprocess, 16.0ms inference, 2.0ms postprocess per image at shape (1, 3, 384, 640)
Results saved to runs\detect\predict10

image 1/1 C:\Users\krish\OneDrive\Desktop\project\data\new_car\data\test\vid_4_600.jpg: 384x640 1 car, 15.0ms
Speed: 2.0ms preprocess, 15.0ms inference, 2.0ms postprocess per image at shape (1, 3, 384, 640)
Results saved to runs\detect\predict10

image 1/1 C:\Users\krish\OneDrive\Desktop\project\data\new_car\data\test\vid_4_620.jpg: 384x640 1 car, 18.0ms
Speed: 2.0ms preprocess, 18.0ms inference, 4.0ms postprocess per image at shape (1, 3, 384, 640)
Results saved to runs\detect\predict10

image 1/1 C:\Users\krish\OneDrive\Desktop\project\data\new_car\data\test\vid_4_640.jpg: 384x640 (no detections), 16.5ms
Speed: 2.0ms preprocess, 16.5ms inference, 1.0ms postprocess per image at shape (1, 3, 384, 640)
Results saved to runs\detect\predict10

image 1/1 C:\Users\krish\OneDrive\Desktop\project\data\new_car\data\test\vid_4_660.jpg: 384x640 (no detections), 16.0ms
Speed: 2.0ms preprocess, 16.0ms inference, 1.0ms postprocess per image at shape (1, 3, 384, 640)
Results saved to runs\detect\predict10

image 1/1 C:\Users\krish\OneDrive\Desktop\project\data\new_car\data\test\vid_4_680.jpg: 384x640 (no detections), 14.0ms
Speed: 2.0ms preprocess, 14.0ms inference, 1.0ms postprocess per image at shape (1, 3, 384, 640)
Results saved to runs\detect\predict10

image 1/1 C:\Users\krish\OneDrive\Desktop\project\data\new_car\data\test\vid_4_700.jpg: 384x640 1 car, 15.5ms
Speed: 2.0ms preprocess, 15.5ms inference, 2.0ms postprocess per image at shape (1, 3, 384, 640)
Results saved to runs\detect\predict10

image 1/1 C:\Users\krish\OneDrive\Desktop\project\data\new_car\data\test\vid_4_720.jpg: 384x640 2 cars, 13.0ms
Speed: 1.0ms preprocess, 13.0ms inference, 2.0ms postprocess per image at shape (1, 3, 384, 640)
Results saved to runs\detect\predict10

image 1/1 C:\Users\krish\OneDrive\Desktop\project\data\new_car\data\test\vid_4_740.jpg: 384x640 1 car, 14.0ms
Speed: 2.0ms preprocess, 14.0ms inference, 2.0ms postprocess per image at shape (1, 3, 384, 640)
Results saved to runs\detect\predict10

image 1/1 C:\Users\krish\OneDrive\Desktop\project\data\new_car\data\test\vid_4_760.jpg: 384x640 (no detections), 19.0ms
Speed: 2.0ms preprocess, 19.0ms inference, 1.0ms postprocess per image at shape (1, 3, 384, 640)
Results saved to runs\detect\predict10

image 1/1 C:\Users\krish\OneDrive\Desktop\project\data\new_car\data\test\vid_4_780.jpg: 384x640 (no detections), 15.0ms
Speed: 2.0ms preprocess, 15.0ms inference, 1.0ms postprocess per image at shape (1, 3, 384, 640)
Results saved to runs\detect\predict10

image 1/1 C:\Users\krish\OneDrive\Desktop\project\data\new_car\data\test\vid_4_800.jpg: 384x640 (no detections), 16.5ms
Speed: 2.0ms preprocess, 16.5ms inference, 1.0ms postprocess per image at shape (1, 3, 384, 640)
Results saved to runs\detect\predict10

image 1/1 C:\Users\krish\OneDrive\Desktop\project\data\new_car\data\test\vid_4_820.jpg: 384x640 1 car, 14.0ms
Speed: 2.0ms preprocess, 14.0ms inference, 2.0ms postprocess per image at shape (1, 3, 384, 640)
Results saved to runs\detect\predict10

image 1/1 C:\Users\krish\OneDrive\Desktop\project\data\new_car\data\test\vid_4_860.jpg: 384x640 1 car, 13.0ms
Speed: 2.0ms preprocess, 13.0ms inference, 3.0ms postprocess per image at shape (1, 3, 384, 640)
Results saved to runs\detect\predict10

image 1/1 C:\Users\krish\OneDrive\Desktop\project\data\new_car\data\test\vid_4_880.jpg: 384x640 1 car, 13.0ms
Speed: 2.0ms preprocess, 13.0ms inference, 2.0ms postprocess per image at shape (1, 3, 384, 640)
Results saved to runs\detect\predict10

image 1/1 C:\Users\krish\OneDrive\Desktop\project\data\new_car\data\test\vid_4_900.jpg: 384x640 1 car, 13.0ms
Speed: 2.0ms preprocess, 13.0ms inference, 2.0ms postprocess per image at shape (1, 3, 384, 640)
Results saved to runs\detect\predict10

image 1/1 C:\Users\krish\OneDrive\Desktop\project\data\new_car\data\test\vid_4_920.jpg: 384x640 1 car, 12.0ms
Speed: 2.0ms preprocess, 12.0ms inference, 2.0ms postprocess per image at shape (1, 3, 384, 640)
Results saved to runs\detect\predict10

image 1/1 C:\Users\krish\OneDrive\Desktop\project\data\new_car\data\test\vid_4_940.jpg: 384x640 2 cars, 13.0ms
Speed: 2.0ms preprocess, 13.0ms inference, 2.0ms postprocess per image at shape (1, 3, 384, 640)
Results saved to runs\detect\predict10

image 1/1 C:\Users\krish\OneDrive\Desktop\project\data\new_car\data\test\vid_4_960.jpg: 384x640 2 cars, 13.0ms
Speed: 2.0ms preprocess, 13.0ms inference, 2.0ms postprocess per image at shape (1, 3, 384, 640)
Results saved to runs\detect\predict10

image 1/1 C:\Users\krish\OneDrive\Desktop\project\data\new_car\data\test\vid_4_980.jpg: 384x640 1 car, 14.0ms
Speed: 1.0ms preprocess, 14.0ms inference, 2.5ms postprocess per image at shape (1, 3, 384, 640)
Results saved to runs\detect\predict10
In [1]:
# Displaying the test images along with their predicted bounding boxes
from PIL import Image
import os
import matplotlib.pyplot as plt

# Directory of the test images
image_dir = 'C:\\Users\\krish\\Foundations of Machine Learning\\runs\\detect\\predict2'

# Get a list of image file names in the directory
image_files = [f for f in os.listdir(image_dir) if f.endswith(('.jpg', '.jpeg', '.png', '.gif', '.bmp'))]

# Loop through the image files and display them
for image_file in image_files:
    # Construct the full path to the image file
    image_path = os.path.join(image_dir, image_file)

    # Open and display the image using Pillow and matplotlib
    image = Image.open(image_path)
    plt.imshow(image)
    plt.axis('off')  
    plt.title(image_file) 

Evaluvation Metrics¶

In [4]:
import pandas as pd

# Importing the csv wiwth ground truth coordinates
df = pd.read_csv(r"C:\Users\krish\OneDrive\Desktop\project\data\new_car\data\testin_coors.csv")
In [5]:
# Extracting the ground truth coordinates
groundtruth_coordinates = []

# Loop through the DataFrame and extract values into lists of lists
for index, row in df.iterrows():
    xmin = row['xmin']
    ymin = row['ymin']
    xmax = row['xmax']
    ymax = row['ymax']
    
    # Create a list with these values and append it to bbox_list
    bbox = [xmin, ymin, xmax, ymax]
    groundtruth_coordinates.append(bbox)
In [6]:
# Flattening the prediction coordinates list
prediction_coordinates = [[item.item() for item in tensor] for sublist in predicted_boxes for tensor in sublist]
In [7]:
# Used CHATGPT to figure out to logic

def calculate_iou(box1, box2):
    # Convert "x, y, w, h" format to "xmin, ymin, xmax, ymax" format for both boxes
    xmin1, ymin1, xmax1, ymax1 = box1[0], box1[1], box1[0] + box1[2], box1[1] + box1[3]
    xmin2, ymin2, xmax2, ymax2 = box2[0], box2[1], box2[0] + box2[2], box2[1] + box2[3]

    # Calculate the coordinates of the intersection rectangle
    x_intersection = max(0, min(xmax1, xmax2) - max(xmin1, xmin2))
    y_intersection = max(0, min(ymax1, ymax2) - max(ymin1, ymin2))

    # Calculate the areas of the intersection and the union
    intersection_area = x_intersection * y_intersection
    area1 = box1[2] * box1[3]
    area2 = box2[2] * box2[3]
    union_area = area1 + area2 - intersection_area

    # Calculate IoU
    iou = intersection_area / union_area
    return iou
In [23]:
iou_scores = []
for gt, pred in zip(groundtruth_coordinates, prediction_coordinates):
    if calculate_iou(gt, pred) > 0.40:
        iou_scores.append(calculate_iou(gt, pred))
        
print('The Avergae IOU Score is : ', (sum(iou_scores)/len(iou_scores)) * 100)
The Avergae IOU Score is :  73.46242652145749
In [ ]: